package inet.app.vod.view;

import inet.app.vod.activity.R;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.inet.app.framework.device.DeviceUtil;
import com.inet.app.framework.util.hardware.ConvertUtil;

public class MyViewPagerIndicator extends RelativeLayout {

	public final static int SIZE_INDICATOR = 7;
	final static String TAG = "MyViewPagerIndicator";

	ViewPager mViewPager;
	HorizontalScrollView mIndicatorScroll;
	ArrayList<View> mIndicatorView = new ArrayList<View>();
	LinearLayout mIndicatorLayout;
	Context mContext;

	static int mSizeIndicatorPx;
	static LinearLayout.LayoutParams mIndicatorParams;
	static int mWidthDevice;
	int mColorSelect = -9;
	int mColorDefault = -9;

	int mCurPosition = 0; // page current

	public MyViewPagerIndicator(Context context) {
		super(context);
		initLayout(context, R.layout.view_myviewpager_indicator);
	}

	public MyViewPagerIndicator(Context context, AttributeSet attrs) {
		super(context, attrs);
		initLayout(context, R.layout.view_myviewpager_indicator);
	}

	public MyViewPagerIndicator(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		initLayout(context, R.layout.view_myviewpager_indicator);
	}

	public void initLayout(Context context, int resId) {
		LayoutInflater layoutInflate = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layoutInflate.inflate(resId, this, true);
	}

	public void attachToViewPager(ViewPager viewPager,
			final OnPageChangeListener pageChangeListener) {

		viewPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int position) {
				resetIndicatorView(position);
				if (pageChangeListener != null) {
					pageChangeListener.onPageSelected(position);
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
				if (pageChangeListener != null) {
					pageChangeListener.onPageScrolled(arg0, arg1, arg2);
				}
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
				if (pageChangeListener != null) {
					pageChangeListener.onPageScrollStateChanged(arg0);
				}
			}
		});
		mViewPager = viewPager;
		init();
	}

	public void attachToViewPager(ViewPager viewPager) {
		attachToViewPager(viewPager, null);
	}

	/**
	 * Init view for view page indicator
	 * 
	 * @param sizePage
	 *            number page of view pager
	 */
	private void init() {
		int sizePage = mViewPager.getAdapter().getCount();
		if (sizePage == 0) {
			return;
		}
		mContext = getContext();
		mIndicatorLayout = (LinearLayout) findViewById(R.id.indicator_layout);
		mIndicatorScroll = (HorizontalScrollView) findViewById(R.id.indicator_scroll);

		/* initialize indicator layout */
		View indicator;

		// initialize only once
		mSizeIndicatorPx = (int) ConvertUtil.convertDpToPixel(mContext,
				SIZE_INDICATOR);
		mIndicatorParams = new LinearLayout.LayoutParams(mSizeIndicatorPx,
				mSizeIndicatorPx);
		mIndicatorParams.setMargins(mSizeIndicatorPx / 2, 0,
				mSizeIndicatorPx / 2, 0);
		if (mColorSelect == -9) {
			mColorSelect = getResources().getColor(R.color.yellow_text);
		}
		if (mColorDefault == -9) {
			mColorDefault = getResources().getColor(R.color.black);
		}
		mWidthDevice = DeviceUtil.getDeviceInfo((Activity) mContext).getWidth();

		for (int i = 0; i < sizePage; i++) {
			indicator = new View(mContext);
			indicator.setLayoutParams(mIndicatorParams);
			indicator.setTag(i);
			if (i == 0) {
				indicator.setBackgroundColor(mColorSelect);
			} else {
				indicator.setBackgroundColor(mColorDefault);
			}
			final int numPage = i;
			indicator.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// Click to switch page
					mViewPager.setCurrentItem(numPage, true);
				}
			});
			mIndicatorView.add(indicator);
			mIndicatorLayout.addView(indicator);
		}
	}

	/**
	 * Update indicators when view pager change page
	 * 
	 * @param indexSelect
	 *            index of page is selected
	 */
	private void resetIndicatorView(int indexSelect) {

		// Change current indicator select
		View curViewSelect = (View) mIndicatorLayout
				.findViewWithTag(mCurPosition);
		if (curViewSelect != null) {
			curViewSelect.setBackgroundColor(mColorDefault);
		}
		View targetViewSelect = (View) mIndicatorLayout
				.findViewWithTag(indexSelect);
		if (targetViewSelect != null) {
			targetViewSelect.setBackgroundColor(mColorSelect);
		}

		int ind = 0;
		int size = mIndicatorView.size();

		int indFirstViewVisible = 0;
		boolean isCheckFirstViewVisible = true;

		int indLastViewVisible = size - 1;
		boolean isCheckLastviewVisible = false;

		for (View indicator : mIndicatorView) {

			if (isCheckFirstViewVisible) {
				if (indicator.getGlobalVisibleRect(new Rect())) {
					indFirstViewVisible = ind;
					isCheckFirstViewVisible = false;

					// start calculate last visible
					indLastViewVisible = indFirstViewVisible;
					isCheckLastviewVisible = true;
				}
			}

			if (isCheckLastviewVisible) {
				if (indicator.getGlobalVisibleRect(new Rect())) {
					indLastViewVisible++;
				} else {
					indLastViewVisible--;
					isCheckLastviewVisible = false;
				}
			}
			ind++;
		}

		if (indexSelect == indFirstViewVisible) {
			// scroll for first view
			mIndicatorScroll.smoothScrollTo(mIndicatorScroll.getScrollX()
					- mWidthDevice / 2, 0);
		}

		if (indexSelect == indLastViewVisible) {
			// scroll for last view
			mIndicatorScroll.smoothScrollTo(mIndicatorScroll.getScrollX()
					+ mWidthDevice / 2, 0);
		}
		mCurPosition = indexSelect;
	}

	public void scrollTo(int x, int y) {
		if (mIndicatorScroll != null) {
			mIndicatorScroll.scrollTo(x, y);
		}
	}

	public void setColorSelect(int colorSelect) {
		this.mColorSelect = colorSelect;
	}

	public void setColorDefault(int colorDefault) {
		this.mColorDefault = colorDefault;
	}
}
